package erfgame.core.world.terrain.color;


import erfgame.core.util.ColorUtils;
import erfgame.core.world.terrain.TerrainColorSource;

public final class QuickMappedTerrainColorSource implements TerrainColorSource {
	
	private int[][][] colorMap;
	private int[][][] maxColorMap;
	private int[][][] minColorMap;
	
	private int widthMask;
	private int heightMask;
	private int depthMask;
	
	public QuickMappedTerrainColorSource(int[][][] minColorMap, int[][][] maxColorMap, int[][][] colorMap, int widthMask, int heightMask, int depthMask) {
		this.colorMap = colorMap;
		this.minColorMap = minColorMap;
		this.maxColorMap = maxColorMap;
		this.widthMask = widthMask;
		this.heightMask = heightMask;
		this.depthMask = depthMask;
	}

	public int getColor(int x, int y, int z, int mult, int div, int dCeiling) {
		int mx = x & this.widthMask;
		int my = y & this.heightMask;
		int mz = z & this.depthMask;
		int middle = this.colorMap[mx][my][mz];
		int color;
		if( mult > 0 ) {
			int extreme = this.maxColorMap[mx][my][mz];
			color = ColorUtils.multiplyColor(middle, extreme, mult, div );
		} else {
			int extreme = this.minColorMap[mx][my][mz];
			color = ColorUtils.multiplyColor(middle, extreme, -mult, div );
		}
		return color;
	}

}
